package com.study.ctripapollo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.study.ctripapollo.entity.User;
import com.study.ctripapollo.entity.common.Result;
import com.study.ctripapollo.enums.user.SexEnum;
import com.study.ctripapollo.service.IUserService;
import com.study.ctripapollo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author flwu
 * @since 2020-02-17
 */
@RestController
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户注册
     * @param params
     * @return
     */
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public Result register(@RequestBody Map<String, String> params) {
        try {
            User user = new User();
            if (StringUtils.isNotBlank(params.get("name")) && StringUtils.isNotBlank(params.get("username")) &&
                    StringUtils.isNotBlank(params.get("password")) && SexEnum.getSexEnum(Integer.parseInt(params.get("sex"))) != null &&
                    Integer.parseInt(params.get("age")) > 0) {
                user.setName(params.get("name"));
                user.setUsername(params.get("username"));
                String md5Password = DigestUtils.md5DigestAsHex(params.get("password").getBytes());
                user.setPassword(md5Password);
                user.setAge(Integer.parseInt(params.get("age")));
                user.setSex(Integer.parseInt(params.get("sex")));
                Integer userId = iUserService.addUser(user);
                if (userId > 0) {
                    User userInfo = iUserService.getUserInfoById(userId);
                    return ResultUtil.success(userInfo);
                } else {
                    return ResultUtil.fail("数据库执行失败");
                }
            } else {
                return ResultUtil.fail("参数错误");
            }
        } catch (Exception e) {
            return ResultUtil.exception(e.getMessage());
        }
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/{id}")
    public Result getUserInfo(@PathVariable("id") Integer id) {
        try {
            User userInfo = iUserService.getUserInfoById(id);
            return ResultUtil.success(userInfo);
        } catch (Exception e){
            return ResultUtil.exception(e.getMessage());
        }
    }

    /**
     * 根据i条件查询用户列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/user/list", method = RequestMethod.POST)
    public Result getUserList(@RequestBody Map<String, String> params) {
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("sex", Integer.parseInt(params.get("sex")));
            queryWrapper.likeRight(true,  "name", params.get("name"));
            List<User> userList = iUserService.getUserList(queryWrapper);
            return ResultUtil.success(userList);
        } catch (Exception e) {
            return ResultUtil.fail("网络错误");
        }
    }

    /**
     * 查询用户列表带分页
     * @return
     * @RequestParam(name = "page", required = false, defaultValue = "1") Integer page
     */
    @RequestMapping(value = "/user/search", method = RequestMethod.POST)
    public Result search(@RequestBody Map<String, String> params) {
        try {
            IPage<User> userList = iUserService.getUserListByPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("pageSize")), params);
            return ResultUtil.success(userList);
        } catch (Exception e) {
            return ResultUtil.exception(e.getMessage());
        }
    }
}
